package com.hb.trade.server.project.file.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.parent.MPCommonEntity;
import com.hb.core.pojo.project.file.dto.ReviewEvaluationFormDTO;
import com.hb.core.pojo.project.file.entity.ReviewEvaluationForm;
import com.hb.core.pojo.project.file.mapper.ReviewEvaluationFormMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.trade.server.project.file.service.ReviewEvaluationFormService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 评审表格 服务实现类
 * </p>
 *
 * @author zhongdl
 * @since 2022-04-19
 */
@Service
public class ReviewEvaluationFormServiceImpl extends ServiceImpl<ReviewEvaluationFormMapper, ReviewEvaluationForm> implements ReviewEvaluationFormService {

    /**
     * @param sectionId:标段id
     * @param fileId:招标文件id
     * @Description:根据标段和招标文件查询表格记录
     * @Author: zhongdl
     * @Date:2022/4/19 11:08
     * @return: {@link java.util.List<com.hb.core.pojo.project.file.dto.ReviewEvaluationFormDTO> }
     **/
    @Override
    public List<ReviewEvaluationFormDTO> listReviewEvaluationGenerate(Long sectionId, Long fileId) {
        List<ReviewEvaluationFormDTO> list = CollUtil.list(false);
        QueryWrapper<ReviewEvaluationForm> wrapper = ReviewEvaluationForm.wrapper();
        wrapper.eq("section_id",sectionId)
                .eq(fileId != null,"file_id",fileId)
                .orderByAsc("sort");
        List<ReviewEvaluationForm> reviewEvaluationForms = list(wrapper);
        if (CollUtil.isEmpty(reviewEvaluationForms)){
            return list;
        }
        for (ReviewEvaluationForm reviewEvaluationForm : reviewEvaluationForms) {
            ReviewEvaluationFormDTO reviewEvaluationFormDTO = reviewEvaluationForm.to(ReviewEvaluationFormDTO.class);
            list.add(reviewEvaluationFormDTO);
        }
        return list;
    }

    @Override
    public Long getReviewEvaluationFormDTO(Long sectionId, Long fileId, Integer formType, Integer firstReviewItemType,Long basicsSecondId,Long expertUser) {
        QueryWrapper<ReviewEvaluationForm> wrapper = ReviewEvaluationForm.wrapper();
        wrapper.eq("section_id",sectionId)
                .eq(fileId!=null,"file_id",fileId)
                .eq("form_type",formType)
                .eq(basicsSecondId != null,"basics_second_id",basicsSecondId)
                .eq("first_review_item_type",firstReviewItemType)
                .eq(expertUser != null,"expert_user",expertUser).last("limit 1");
        ReviewEvaluationForm reviewEvaluationForm = getOne(wrapper);
        return reviewEvaluationForm == null ? null : reviewEvaluationForm.getId();
    }
}
